package com.my.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.*;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.my.R;
import com.my.util.ApiUtil;

public class AskLeaveActivity extends AppCompatActivity {

    boolean isStart = false;
    Spinner askLeaveType;
    DatePicker datePicker;
    Button submitAskLeave;
    TextView selectAskLeaveStart;
    TextView selectAskLeaveEnd;
    EditText askLeaveRemarks;
    JSONObject params = new JSONObject();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ask_leave);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
        }
        askLeaveType = findViewById(R.id.ask_leave_type);
        datePicker = findViewById(R.id.date_picker);
        selectAskLeaveStart = findViewById(R.id.select_ask_leave_start);
        selectAskLeaveEnd = findViewById(R.id.select_ask_leave_end);
        submitAskLeave = findViewById(R.id.submit_ask_leave);
        askLeaveRemarks = findViewById(R.id.ask_leave_remarks);
        selectAskLeaveStart.setOnClickListener(view -> {
            isStart = true;
            datePicker.setVisibility(View.VISIBLE);
            submitAskLeave.setVisibility(View.GONE);
        });
        selectAskLeaveEnd.setOnClickListener(view -> {
            isStart = false;
            datePicker.setVisibility(View.VISIBLE);
            submitAskLeave.setVisibility(View.GONE);
        });
        datePicker.setOnDateChangedListener((v, y, m, d) -> {
            if(isStart) {
                params.putOpt("startTime", y + "-" + (m + 1) + "-" + d);
                selectAskLeaveStart.setText(y + "-" + (m + 1) + "-" + d);
            }else {
                params.putOpt("endTime", y + "-" + (m + 1) + "-" + d);
                selectAskLeaveEnd.setText(y + "-" + (m + 1) + "-" + d);
            }
            datePicker.setVisibility(View.GONE);
            submitAskLeave.setVisibility(View.VISIBLE);
        });
        Handler handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                JSONObject rest = ((JSONObject) msg.obj);
                Toast.makeText(AskLeaveActivity.this, rest.getStr("msg"), Toast.LENGTH_LONG).show();
                if(rest.getInt("code") == 200) {
                    onBackPressed();
                }
            }
        };
        submitAskLeave.setOnClickListener(view -> {
            params.putOpt("type", askLeaveType.getSelectedItemPosition() + 1);
            params.putOpt("remarks", askLeaveRemarks.getText());
            SharedPreferences preferences = this.getSharedPreferences("LOGIN_DATA", Context.MODE_PRIVATE);
            JSONObject user = new JSONObject(preferences.getString("user", null));
            params.putOpt("userId", user.getInt("id"));
            if(params.size() < 5) {
                Toast.makeText(this, "请完善参数", Toast.LENGTH_LONG).show();
                return;
            }
            new Thread(() -> {
                Message message = new Message();
                String body = HttpRequest.post(ApiUtil.HTTP_ADDRESS + "askLeave/add").body(params.toString()).execute().body();
                message.obj = new JSONObject(body);
                handler.sendMessage(message);
            }).start();
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == 16908332) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
}